package com.minaf.fragmentdemo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    Button btn;
    FragmentManager fragmentManager;
    public List<Fragment> mList;
    int index=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragmentManager = getSupportFragmentManager();
        mList = new ArrayList<>();
        mList.add(new FirstFragment());
        mList.add(new SecondFragment());

        btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index++;
                fragmentManager.beginTransaction().
                        replace(R.id.container, mList.get(index % mList.size())).
//                        addToBackStack(null).
                        commit();

            }
        });
    }
}
